package com.apestech.api.tables.modul;

import com.apestech.framework.json.JSONObject;

import java.util.Date;

/**
 * @author hup
 * @create 2019-02-15-15:00
 */
public class SeqField {
    public class Id {
        private long id;
        private int type=0;  //0:序列;1:表
        private String tableId;
        private String tableName;
        private long range=1;
        private String prefix;
        private String suffix;
        private int padding=6;
        private long num=0;
        private String timestamp;
        private int cycle;
        private String idField;

        public void setField(JSONObject input) throws Exception {
            if(input.get("ID")!=null) id=input.getLongValue("ID");
            if(input.get("TYPE")!=null) type=input.getIntValue("TYPE");
            if(input.get("TABLE_ID")!=null) tableId=input.getString("TABLE_ID");
            if(input.get("TABLE_NAME")!=null) tableName=input.getString("TABLE_NAME");
            if(input.get("PREFIX")!=null) prefix=input.getString("PREFIX");
            else prefix="";
            if(input.get("SUFFIX")!=null) suffix=input.getString("SUFFIX");
            else suffix="";
            if(input.get("RANGE")!=null) range=input.getLongValue("RANGE");
            if(input.get("NUM")!=null) num=input.getLongValue("NUM");
            if(input.get("PADDING")!=null) padding=input.getIntValue("PADDING");
            if(input.get("LAST_UPDATED_STAMP")!=null) timestamp=input.getString("LAST_UPDATED_STAMP");
            if(input.get("CYCLE")!=null) cycle=input.getIntValue("CYCLE");
            idField=input.getString("idField");
        }

        public JSONObject toJSON(){
            JSONObject m=new JSONObject();
            m.put("ID", id);
            m.put("TYPE", type);
            m.put("TABLE_ID", tableId);
            m.put("TABLE_NAME", tableName);
            m.put("RANGE", range);
            m.put("PADDING", padding);
            m.put("PREFIX", prefix);
            m.put("SUFFIX", suffix);
            m.put("NUM", num);
            m.put("CYCLE", cycle);
            m.put("VALID", "1");
            m.put("CREATE_DATE", "SYSDATE");
            if(timestamp==null) m.put("LAST_UPDATED_STAMP", "SYSDATE");
            else m.put("LAST_UPDATED_STAMP", timestamp);
            m.put("LAST_UPDATED_STAMP_NEW", "SYSDATE");
            return m;
        }

        public String getPrefix() {
            return prefix;
        }

        public void setPrefix(String prefix) {
            this.prefix = prefix;
        }

        public String getSuffix() {
            return suffix;
        }

        public void setSuffix(String suffix) {
            this.suffix = suffix;
        }

        public int getPadding() {
            return padding;
        }

        public void setPadding(int padding) {
            this.padding = padding;
        }

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getTableId() {
            return tableId;
        }

        public void setTableId(String tableId) {
            this.tableId = tableId;
        }

        public String getTableName() {
            return tableName;
        }

        public void setTableName(String tableName) {
            this.tableName = tableName;
        }

        public long getRange() {
            return range;
        }

        public void setRange(long range) {
            this.range = range;
        }

        public long getNum() {
            return num;
        }

        public void setNum(long num) {
            this.num = num;
        }

        public String getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(String timestamp) {
            this.timestamp = timestamp;
        }

        public int getCycle() {
            return cycle;
        }

        public void setCycle(int cycle) {
            this.cycle = cycle;
        }

        public String getIdField() {
            return idField;
        }

        public void setIdField(String idField) {
            this.idField = idField;
        }
    }

    public class Range{
        private String key;
        private int newCode;
        private int padding=3;
        private long startNumber;
        private long endNumber;
        private long num;
        private Date timestamp;

        public void setRange(JSONObject input) throws Exception {
            if(input.get("KEY")!=null) key=input.getString("KEY");
            if(input.get("NEW_CODE")!=null) newCode=input.getIntValue("NEW_CODE");
            if(input.get("PADDING")!=null) padding=input.getIntValue("PADDING");
            if(input.get("START_NUMBER")!=null) startNumber=input.getLongValue("START_NUMBER");
            if(input.get("END_NUMBER")!=null) endNumber=input.getLongValue("END_NUMBER");
            if(input.get("NUM")!=null) num=input.getLongValue("NUM");
            if(input.get("LAST_UPDATED_STAMP")!=null) timestamp=input.getDate("LAST_UPDATED_STAMP");
        }

        public JSONObject toJSON(){
            JSONObject m=new JSONObject();
            m.put("KEY", key);
            m.put("NEW_CODE", newCode);
            m.put("PADDING", padding);
            m.put("START_NUMBER", startNumber);
            m.put("END_NUMBER", endNumber);
            m.put("NUM", num);
            m.put("CREATE_DATE", "SYSDATE");
            if(timestamp==null) m.put("LAST_UPDATED_STAMP", "SYSDATE");
            else m.put("LAST_UPDATED_STAMP", timestamp);
            m.put("LAST_UPDATED_STAMP_NEW", "SYSDATE");
            return m;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public int getNewCode() {
            return newCode;
        }

        public void setNewCode(int newCode) {
            this.newCode = newCode;
        }

        public int getPadding() {
            return padding;
        }

        public void setPadding(int padding) {
            this.padding = padding;
        }

        public long getStartNumber() {
            return startNumber;
        }

        public void setStartNumber(long startNumber) {
            this.startNumber = startNumber;
        }

        public long getEndNumber() {
            return endNumber;
        }

        public void setEndNumber(long endNumber) {
            this.endNumber = endNumber;
        }

        public long getNum() {
            return num;
        }

        public void setNum(long num) {
            this.num = num;
        }

        public Date getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(Date timestamp) {
            this.timestamp = timestamp;
        }
    }

    private Id id=new Id();
    private JSONObject range=new JSONObject();

    public String getPrefix() {
        return id.getPrefix();
    }

    public void setPrefix(String prefix) {
        id.setPrefix(prefix);
    }

    public String getSuffix() {
        return id.getSuffix();
    }

    public void setSuffix(String suffix) {
        id.setSuffix(suffix);
    }

    public int getPadding() {
        return id.getPadding();
    }

    public void setPadding(int padding) {
        id.setPadding(padding);
    }

    public long getId() {
        return id.getId();
    }

    public void setId(long id) {
        this.id.setId(id);
    }

    public int getType() {
        return id.getType();
    }

    public void setType(int type) {
        id.setType(type);
    }

    public String getTableId() {
        return id.getTableId();
    }

    public void setTableId(String tableId) {
        id.setTableId(tableId);
    }

    public String getTableName() {
        return id.getTableName();
    }

    public void setTableName(String tableName) {
        id.setTableName(tableName);
    }

    public long getRange() {
        return id.getRange();
    }

    public void setRange(long range) {
        id.setRange(range);
    }

    public long getNum() {
        return id.getNum();
    }

    public void setNum(long num) {
        id.setNum(num);
    }

    public String getTimestamp() {
        return id.getTimestamp();
    }

    public void setTimestamp(String timestamp) {
        id.setTimestamp(timestamp);
    }

    public int getCycle() {
        return id.getCycle();
    }

    public void setCycle(int cycle) {
        id.setCycle(cycle);
    }

    public void setField(JSONObject input) throws Exception {
        id.setField(input);
    }

    public String getIdField() {
        return id.getIdField();
    }

    public void setIdField(String idField) {
        id.setIdField(idField);
    }

    public JSONObject toJSON(){
        return id.toJSON();
    }

    public void setRange(String key, JSONObject input) throws Exception {
        Range r=getRange(key);
        r.setRange(input);
        range.put(key, r);
    }

    public JSONObject toJson(String key) throws Exception {
        return getRange(key).toJSON();
    }

    public Range getRange(String key){
        if(range.get(key)==null) return new Range();
        return (Range) range.get(key);
    }

}
